package com.jn.okhttp.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.jn.okhttp.common.WeakRrfHandler;
import com.jn.okhttp.contract.ZidianContract;

import com.jn.okhttp.entity.ZidianEntity;
import com.jn.okhttp.utils.LogUtils;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ZidianPresenter implements ZidianContract.IZidianPresenter {

    public static final int HTTP_SUCCESS = 0;
    public static final int HTTP_FAIL = 1;
    private ZidianContract.IZidianView mMainview;
    private Context mContext;
    private OkHttpClient mClient;

    public ZidianPresenter(ZidianContract.IZidianView mMainview){
            if (mMainview == null) {
                throw new IllegalArgumentException("mMainView cannot be null");
            }
            this.mMainview = mMainview;
            mClient = new OkHttpClient();

    }



    @Override
    public void getData(String text) {
        LogUtils.d("ZidianPresenter","text =="+text);
        String url ="https://api.oioweb.cn/api/txt/dict?text="+text;
        Request request =new Request.Builder()
                .url(url)
                .get()
                .build();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response response =mClient.newCall(request).execute();
                    if (response.isSuccessful()){
                        String body =response.body().string();
                        LogUtils.d("ZidianPresenter", "onResponse : " + body);
                        Message message =Message.obtain();
                        message.obj =body;
                        message.what =HTTP_SUCCESS;
                        mHandler.sendMessage(message);
                    }
                    else {
                         //Handle request failure
                        Message message =Message.obtain();
                        message.what =HTTP_FAIL;
                        mHandler.sendMessage(message);
                    }
                } catch (IOException e) {
                   e.printStackTrace();
                    Message message = Message.obtain();
                    message.what =HTTP_FAIL;
                    mHandler.sendMessage(message);
                }
            }
        }).start();
    }
    WeakRrfHandler<ZidianPresenter>mHandler =new WeakRrfHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case HTTP_SUCCESS:
                    String body =(String) msg.obj;
                    Gson gson =new Gson();
                    ZidianEntity zidianEntity = gson.fromJson(body, ZidianEntity.class);
                    if (zidianEntity != null && mMainview != null) {
                        mMainview.showData(zidianEntity);
                    }
                    break;
                case HTTP_FAIL:
                    if (mMainview !=null){
                        mMainview.showError("请求失败");
                }
                    break;
            }
            return true;
        }
    });
}
